//
//  CustomLayout.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/31.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

class CustomLayout: UICollectionViewLayout {

    //间隔2
    let padding : CGFloat = 2.0
    var litterCellWidth : CGFloat {
       return (SCREEN_W - padding * 3.0) * 0.5
    }
    
    var bigCellWidth : CGFloat {
        return (SCREEN_W - padding * 2.0)
    }
    
    var sectionHeight : CGFloat {
        return (litterCellWidth * 2) + (padding * 3) + bigCellWidth
    }
    
    var contentMAXHeight : CGFloat = 0
    
    var attributes : [UICollectionViewLayoutAttributes] = []
    
    override func prepare() {
        super.prepare()
        
        var start_y : CGFloat = 0.0
        var start_x : CGFloat = 0.0
        var width : CGFloat = 0.0
        let cellCount = self.collectionView!.numberOfItems(inSection: 0)
        for i in 0..<cellCount {
            //注意这里是创建一个Attribute，从indexpath
            let attri = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: i, section: 0))
            let section = i / 5
            print(section)
            let index = i % 5
            var indexHeight : CGFloat = 0
            switch index {
                case 0:
                    indexHeight = 0
                    start_x = padding
                case 1:
                    indexHeight = padding + bigCellWidth
                    start_x = padding
                case 2:
                    indexHeight = padding + bigCellWidth
                    start_x = padding * 2 + litterCellWidth
                case 3:
                    indexHeight = padding * 2 + bigCellWidth + litterCellWidth
                    start_x = padding
                case 4:
                    indexHeight = padding * 2 + bigCellWidth + litterCellWidth
                    start_x = padding * 2 + litterCellWidth
                default: break
            }
            start_y = CGFloat(section) * sectionHeight + indexHeight
            width = index == 0 ? bigCellWidth : litterCellWidth
            let cellFrame = CGRect(x: start_x, y: start_y, width: width, height: width)
            attri.frame = cellFrame
            attributes.append(attri)
            if i == cellCount - 1 {
                contentMAXHeight = start_y + width + 10
            }
        }
    }
    
    //You use this method to provide layout information only for items that have a corresponding cell
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return attributes[indexPath.item]
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        //虽然这里是要求返回在可见rect范围中的视图元素（包含cell， supplementary views, and decoration views）
        // 可以用if(CGRectIntersectsRect(cellFrame, rect))方法来判断一下
        return attributes
    }
    
    override var collectionViewContentSize: CGSize {
        //prepare方法比这方法先调用，于是可以在prepare中先算出来。
        return CGSize(width: SCREEN_W , height: contentMAXHeight)
    }
    
}
